package io.gitee.sfch.chunmi.springframework;

import io.gitee.sfch.chunmi.springframework.argument.ICodeEnum;
import io.gitee.sfch.chunmi.springframework.argument.StringToCodeEnumConverterFactory;
import io.gitee.sfch.chunmi.entity.Pageable;
import io.gitee.sfch.chunmi.springframework.argument.PageableHandlerMethodArgumentResolver;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * 春米的Spring框架Mvc配置，添加{@link Pageable}，{@link ICodeEnum}的参数解析支持
 */
@Configuration
public class ChunmiWebMvcConfigurer implements WebMvcConfigurer {

    @Resource
    private PageableHandlerMethodArgumentResolver pageableHandlerMethodArgumentResolver;
    @Resource
    private StringToCodeEnumConverterFactory stringToCodeEnumConverterFactory;

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        // 分页参数支持
        resolvers.add(pageableHandlerMethodArgumentResolver);
    }

    @Override
    public void addFormatters(@Nonnull FormatterRegistry registry) {
        // 编码枚举作为参数支持
        registry.addConverterFactory(this.stringToCodeEnumConverterFactory);
    }
}
